import { getSiteCountryByUrl } from "@/utils/tools";
import type { TParseSpuHtml, TParseSkuHtml } from "../type";
import { extractProductSpecs } from "./DimensionsAndWeightAndBrand";
import { extractCategories } from "./Category";
import { extractProductDescription } from "./Description";
import { extractSkuSpecs } from "./SkuSpecs";
import { extractMainImages, extractMainImage } from "./MainImage";
import { extractShopName } from "./ShopName";
import { extractProductStock } from "./Stock";
import { getProductPrice } from "./Price";
import { extractRatings } from "./Ratings";
import { extractShippingInfo } from "./ShippingInfo";
import { extractProductTitle } from "./Title";
import { extractNumberRatings } from "./NumberRatings";
import { extractRecentSalesPerformance } from "./RecentSalesPerformance";
import { extractNumberRatingValue } from "./NumberRatingValue";

const parseSpuHtml: TParseSpuHtml = async ({ html, url, collectConfig }) => {
  const { collectPriceType, collectDeliverTimeMethod } = collectConfig;
  const parser = new DOMParser();
  const doc = parser.parseFromString(html, "text/html");
  const marketplace = getSiteCountryByUrl(url);
  // 产品信息
  const { dimensions, weight, brand } = extractProductSpecs(doc, marketplace);

  const { shipping_fee, ship_from, ship_deliver_time, sold_by } =
    await extractShippingInfo({ doc, marketplace, collectDeliverTimeMethod });
  const ratings = extractRatings(doc);
  let number_ratings = extractNumberRatings(doc);
  if(marketplace === "FRA") {
    number_ratings = number_ratings?.replace(/(\d)\s+(\d)/g, "$1$2");
  }
  const number_rating_value = extractNumberRatingValue(doc, marketplace);
  const recent_sales_performance = extractRecentSalesPerformance(doc);
  const price = getProductPrice(doc, marketplace, collectPriceType);
  const stock = extractProductStock(doc, marketplace);
  const shop_name = extractShopName(doc, marketplace);
  const category = extractCategories(doc);
  const title = extractProductTitle(doc);
  // 图片信息
  const main_image = extractMainImage(doc);
  const main_images = extractMainImages(doc);
  // const startTime = performance.now();
  // const ossImageMap: any = await uploadImageToOss([main_image, ...main_images]);
  // console.log("ossImageMap", ossImageMap);
  // const oss_image = ossImageMap[main_image] || main_image;
  // const oss_images = main_images.map((url) => {
  //   return ossImageMap[url] || url;
  // });
  // const endTime = performance.now();
  // console.log("上传图片", ((endTime - startTime)/1000).toFixed(2));
  // 描述
  const description = extractProductDescription(doc);
  // 规格
  const specs = Object.entries(extractSkuSpecs(doc) || {}).map(
    ([key, value]) => {
      return {
        key,
        value,
      };
    }
  );

  return {
    dimensions,
    weight,
    brand: brand || shop_name,
    shipping_fee,
    ship_from,
    ship_deliver_time,
    sold_by,
    ratings,
    recent_sales_performance,
    number_ratings,
    number_rating_value,
    price,
    stock,
    shop_name,
    category,
    title,
    main_image,
    main_images,
    description,
    displaySkuInfo: {
      dimensions,
      weight,
      price,
      stock,
      title,
      specs,
      thumb_url: main_image,
      images: main_images,
    },
    // oss_images,
    // oss_image,
  };
};

const parseSkuHtml: TParseSkuHtml = async ({ html, url, collectConfig }) => {
  const { collectPriceType } = collectConfig;
  const parser = new DOMParser();
  const doc = parser.parseFromString(html, "text/html");
  const marketplace = getSiteCountryByUrl(url);
  // 产品信息
  const { dimensions, weight } = extractProductSpecs(doc, marketplace);
  const price = getProductPrice(doc, marketplace, collectPriceType);
  const stock = extractProductStock(doc, marketplace);
  const title = extractProductTitle(doc);
  // 图片信息
  const thumb_url = extractMainImage(doc);
  const images = extractMainImages(doc);
  // 规格
  const specs = Object.entries(extractSkuSpecs(doc) || {}).map(
    ([key, value]) => {
      return {
        key,
        value,
      };
    }
  );

  // const ossImageMap: any = await uploadImageToOss([thumb_url, ...images]);
  // const oss_thumb_url = ossImageMap[thumb_url] || thumb_url;
  // const oss_images = images.map((url) => {
  //   return ossImageMap[url] || url;
  // });

  return {
    dimensions,
    weight,
    price,
    stock,
    title,
    specs,
    thumb_url,
    images,
    // oss_images,
    // oss_thumb_url,
  };
};

export default {
  parseSpuHtml,
  parseSkuHtml,
};
